<html>
<head><meta charset="utf-8"><title>How do I compare a DefId to CRATE_HIR_ID? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html">How do I compare a DefId to CRATE_HIR_ID?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203605420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203605420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203605420">(Jul 11 2020 at 17:19)</a>:</h4>
<p>Currently I have the following code: <code>item.def_id.as_local().map(|def_id| self.cx.tcx.hir().as_local_hir_id(def_id)) = Some(hir::CRATE_HIR_ID)</code>. Is there an easier way to do this?</p>



<a name="203605476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203605476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203605476">(Jul 11 2020 at 17:21)</a>:</h4>
<p>(deleted)</p>



<a name="203605480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203605480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203605480">(Jul 11 2020 at 17:21)</a>:</h4>
<p>(sorry, I keep replying in the wrong thread)</p>



<a name="203605482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203605482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203605482">(Jul 11 2020 at 17:21)</a>:</h4>
<p>hmm maybe <code>def_id.is_top_level_module()</code>?</p>



<a name="203605557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203605557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203605557">(Jul 11 2020 at 17:23)</a>:</h4>
<p>Yes, that should work.</p>



<a name="203620128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620128">(Jul 12 2020 at 00:32)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> you generally shouldn't need to convert a <code>DefId</code> into a <code>HirId</code> unless you plan to grab some HIR nodes. kind of weird that <code>CRATE_HIR_ID</code> even exists tbh</p>



<a name="203620187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620187">(Jul 12 2020 at 00:34)</a>:</h4>
<p>I guess this makes sense but it could be a constant <em>shrug</em> <a href="https://github.com/rust-lang/rust/blob/master/src/librustc_span/def_id.rs#L167">https://github.com/rust-lang/rust/blob/master/src/librustc_span/def_id.rs#L167</a></p>



<a name="203620251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620251">(Jul 12 2020 at 00:36)</a>:</h4>
<p>This is left over from when intra-doc links were first written and people assumed they would always be to within the local crate</p>



<a name="203620268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620268">(Jul 12 2020 at 00:37)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/73101/commits/b11656273c16c647c367ffa95203722cd3cf41b3#diff-2bcad7b16b56ef2ebb92f8e60ae33773R475">https://github.com/rust-lang/rust/pull/73101/commits/b11656273c16c647c367ffa95203722cd3cf41b3#diff-2bcad7b16b56ef2ebb92f8e60ae33773R475</a></p>



<a name="203620272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620272">(Jul 12 2020 at 00:37)</a>:</h4>
<p>sure, what I mean is that <code>HirId</code> only makes sense if you actually go poking at the HIR</p>



<a name="203620274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620274">(Jul 12 2020 at 00:37)</a>:</h4>
<p>most of the time, having a <code>DefId</code> is the "better" thing to have</p>



<a name="203620279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620279">(Jul 12 2020 at 00:37)</a>:</h4>
<p>so a <code>HirId</code> is like a "downgrade"</p>



<a name="203620323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620323">(Jul 12 2020 at 00:38)</a>:</h4>
<p>(it's also not free to convert to it, for now)</p>



<a name="203620327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620327">(Jul 12 2020 at 00:38)</a>:</h4>
<p>hmm now I'm wondering if this code will break if you implement a foreign trait for a type in a different module <a href="https://github.com/rust-lang/rust/blob/b11656273c16c647c367ffa95203722cd3cf41b3/src/librustdoc/passes/collect_intra_doc_links.rs#L298">https://github.com/rust-lang/rust/blob/b11656273c16c647c367ffa95203722cd3cf41b3/src/librustdoc/passes/collect_intra_doc_links.rs#L298</a></p>



<a name="203620676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620676">(Jul 12 2020 at 00:49)</a>:</h4>
<p>it's <code>and_then</code></p>



<a name="203620680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620680">(Jul 12 2020 at 00:49)</a>:</h4>
<p>so it would just end up here <a href="https://github.com/rust-lang/rust/blob/b11656273c16c647c367ffa95203722cd3cf41b3/src/librustdoc/passes/collect_intra_doc_links.rs#L328-L329">https://github.com/rust-lang/rust/blob/b11656273c16c647c367ffa95203722cd3cf41b3/src/librustdoc/passes/collect_intra_doc_links.rs#L328-L329</a></p>



<a name="203620681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620681">(Jul 12 2020 at 00:49)</a>:</h4>
<p>assuming you mean that the <code>as_local_hir_id</code> would return <code>None</code></p>



<a name="203620723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620723">(Jul 12 2020 at 00:50)</a>:</h4>
<p>yeah</p>



<a name="203620726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620726">(Jul 12 2020 at 00:50)</a>:</h4>
<p>ok neat, and the only other place <code>hir()</code> is called is for <code>Self</code> which shouldn't lead to issues</p>



<a name="203620732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20compare%20a%20DefId%20to%20CRATE_HIR_ID%3F/near/203620732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20compare.20a.20DefId.20to.20CRATE_HIR_ID.3F.html#203620732">(Jul 12 2020 at 00:51)</a>:</h4>
<p>and checking if the item is private, which only happens if it's a local item anyway</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>